<template>
<div class="main_box">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button plain size="mini" type="success" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
      <el-row>

        <el-form-item label="第三方名称" prop="thirdName">
          <el-input v-model="dataForm.thirdName"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="appKey">
          <el-input v-model="dataForm.appKey"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="appSecret">
          <el-input v-model="dataForm.appSecret"></el-input>
        </el-form-item>
        <el-form-item label="第三方类型" prop="thirdType">
          <el-select v-model="dataForm.thirdType" placeholder="请选择第三方类型">
            <el-option
              v-for="item in thirdTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <HospitalSelect @hospitalId="getHospitalId" :id="dataForm.hospitalId" v-if="dataForm.thirdType == 1"></HospitalSelect>
        <InstitutionSelect @institutionId="getInstitutionId" :id="dataForm.institutionId" v-if="dataForm.thirdType == 2"></InstitutionSelect>
        <el-form-item label="联系人" prop="linkMan">
          <el-input v-model="dataForm.linkMan"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="linkPhone">
          <el-input v-model="dataForm.linkPhone"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" prop="linkAddr">
          <el-input v-model="dataForm.linkAddr"></el-input>
        </el-form-item>
        <!--<el-form-item label="医院类型" prop="classifyId">
          <ClassifyRegion :classifyValues="dataForm.classifyId" @classifyRegionChange="handleClassifyChange"></ClassifyRegion>
        </el-form-item>-->
      </el-row>

      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import OrganApi from "@/api/organ/organ";
import Region from "@/components/Region";
import HospitalSelect from '@/components/HospitalSelect';
import InstitutionSelect from "@/components/InstitutionSelect";
export default{
  data(){
      return {
        dataForm:{
          thirdId: "",
          thirdName: "",
          appKey: "",
          appSecret: "",
          thirdType: "",
          hospitalId: "",
          institutionId: "",
          linkMan: "",
          linkPhone: "",
          linkAddr: ""
        },
        loadingIns:false,
        thirdTypeList: [
          { label: '医院', value: 1 },
          { label: '医检', value: 2 },
          { label: '其他', value: 10 }
        ],
        roleRules: {
          appKey: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            { max: 50, message: '用户名长度不超过50位数字', trigger: 'blur' }
          ],
          appSecret: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { max: 200, message: '密码长度不超过200位数字', trigger: 'blur' }
          ],
          thirdName: [
            { required: true, message: "请输入第三方名称", trigger: "blur" },
            { max: 30, message: '第三方名称长度不超过30个字', trigger: 'blur' }
          ],
          thirdType:[
            { required: true, message: "请选择第三方类型", trigger: "change" }
          ]
        }
      }
  },
  components: {
    Region,
    HospitalSelect,
    InstitutionSelect
  },
  methods: {
    init(){
      if (this.$route.query.id) {
        this.dataForm.thirdId = this.$route.query.id
        this.getData()
      }
    },
    getHospitalId(val) {
      this.dataForm.hospitalId = val
    },
    getInstitutionId(val) {
      this.dataForm.institutionId = val
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.thirdId){
                this.editData();
              }else{
                this.addData();
              }
          });
        }
      })
    },
    addData() {
      OrganApi.addThirdAuth(this.dataForm)
      .then(data => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      OrganApi.editThirdAuth(this.dataForm)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    getData () {
      OrganApi.getThirdAuthInfo({
        thirdId: this.$route.query.id
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
        if (res.data.thirdType){
          this.dataForm.thirdType = parseInt(res.data.thirdType)
        }
        if (res.data.hospitalId){
          this.dataForm.hospitalId = parseInt(res.data.hospitalId)
        }
        if (res.data.institutionId){
          this.dataForm.institutionId = parseInt(res.data.institutionId)
        }
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/organ/thirdAuthManagement'});
    }
  },
  mounted(){
    this.init()
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
